;
(function () {

  'use strict';

  angular.module('services.dataQualityMng', [])
    .service('dataQualityMngDataDicService', dataQualityMngDataDicService);

    function dataQualityMngDataDicService($http) {

    this.databaseList = [];
    this.tableList = [];
    this.tableColumnList = [];
    this.selectedDBidx = 0;
    this.selectedTableidx = 0;
    this.dbLoading = true;
    this.tableLoading = true;
    this.detailLoading = true;
    this.searchType = 'tableName';
    this.searchField = '';
    var db = '';
    var that = this;

    this.init = function () {
      that.dbLoading = true;
      $http.get('/sas/dataManager/dataQuality/dataDictionary/dataBase/query')
        .then(function (resDB) {
          that.dbLoading = false;
          if (resDB.data.state === '10000') {
            that.databaseList = resDB.data.data;
            that.selectedDBidx = 0;
            that.tableLoading = true;
            $http.post('/sas/dataManager/dataQuality/dataDictionary/table/query', {
              schema: that.databaseList[0].schema,
              table: '',
              column: ''
            }).then(function (resTable) {
                that.tableLoading = false;
                if (resTable.data.state === '10000') {
                  that.tableList = resTable.data.data;
                  db = that.databaseList[0].schema;
                  that.queryTableDetail(0, that.tableList[0].table);
                }
              }
            );
          }
        }
      );
    };

    this.queryTable = function (index, database) {
      db = database;
      that.searchField = '';
      that.searchType = 'tableName';
      that.selectedDBidx = index;
      that.tableLoading = true;
      $http.post('/sas/dataManager/dataQuality/dataDictionary/table/query', {schema: database, table: '', column: ''})
        .then(function (res) {
          that.tableLoading = false;
          if (res.data.state === '10000') {
            that.tableList = res.data.data;
            //light(that.tableList);
            if (that.tableList.length >= 1) {
              that.queryTableDetail(0, that.tableList[0].table);
            } else {
              that.tableColumnList = [];
            }
          }
        }
      );
    };

    this.queryTableDetail = function (index, table) {
      that.selectedTableidx = index;
      that.detailLoading = true;
      $http.post('/sas/dataManager/dataQuality/dataDictionary/column/query', {schema: db, table: table})
        .then(function (res) {
          that.detailLoading = false;
          if (res.data.state === '10000') {
            that.tableColumnList = res.data.data;
            if (that.searchType === 'fieldName') {
              lightField(that.tableColumnList);
            }
          }
        }
      );
    };

    this.search = function () {
      angular.forEach(that.tableList, function (obj) {
        obj.tableLight = false;
        obj.tableLightComment = false;
      });
      angular.forEach(that.tableColumnList, function (obj) {
        obj.fieldLight = false;
        obj.fieldLightComment = false;
      });
      var tValue = '';
      var cValue = '';
      if (that.searchType === 'tableName') {
        tValue = that.searchField;
      } else {
        cValue = that.searchField;
      }
      that.tableLoading = true;
      $http.post('/sas/dataManager/dataQuality/dataDictionary/table/query', {schema: db, table: tValue, column: cValue})
        .then(function (res) {
          that.tableLoading = false;
          if (res.data.state === '10000') {
            that.tableList = res.data.data;
            if (that.searchType === 'tableName') {
              lightTable(that.tableList);
            }
            if (that.tableList.length >= 1) {
              that.queryTableDetail(0, that.tableList[0].table);
            } else {
              that.tableColumnList = [];
            }
          }
        }
      );
    };

    function lightTable(list) {
      if (that.searchField.trim().length >= 1) {
        angular.forEach(list, function (obj) {
          obj.tableLight = obj.table.toLocaleUpperCase().indexOf(that.searchField.toLocaleUpperCase()) >= 0;
          obj.tableLightComment = obj.tableComment.toLocaleUpperCase().indexOf(that.searchField.toLocaleUpperCase()) >= 0;
        });
      } else {
        angular.forEach(list, function (obj) {
          obj.tableLight = false;
          obj.tableLightComment = false;
        });
      }
    }

    function lightField(list) {
      if (that.searchField.trim().length >= 1) {
        angular.forEach(list, function (obj) {
          obj.fieldLight = obj.column.toLocaleUpperCase().indexOf(that.searchField.toLocaleUpperCase()) >= 0;
          obj.fieldLightComment = obj.columnComment.toLocaleUpperCase().indexOf(that.searchField.toLocaleUpperCase()) >= 0;
        });
      } else {
        angular.forEach(list, function (obj) {
          obj.fieldLight = false;
          obj.fieldLightComment = false;
        });
      }
    }
  }

  dataQualityMngDataDicService.$inject = ['$http'];
})();